package com.ruoyi.common.sql;

import lombok.Getter;

public class Table {
    private final String name;
    private String alias;
    @Getter
    private JoinTypeEnum joinType = JoinTypeEnum.LEFT;
    private Condition joinCondition;

    private Table(String name) {
        this.name = name;
    }

    public static Table of(String name) {
        return new Table(name);
    }

    public Table as(String alias) {
        this.alias = alias;
        return this;
    }

    public Table joinType(JoinTypeEnum joinType) {
        this.joinType = joinType;
        return this;
    }

    public Table on(Condition condition) {
        this.joinCondition = condition;
        return this;
    }

    public void build(StringBuilder sql) {
        sql.append(name);
        if (alias != null) {
            sql.append(" ").append(alias);
        }
        if (joinCondition != null) {
            sql.append(" ON ");
            joinCondition.build(sql);
        }
    }

}
